'use strict';

const Controller = require('egg').Controller;
let jwt = require("jsonwebtoken")
class HomeController extends Controller {

  //首页dllist。
  //渲染列表数据。
  async dlList() {
    const { ctx, app } = this
    const data = await app.mysql.select('dlList')
    const { authorization } = ctx.request.header

    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }
  //新房  求租  求购
  async hotRoom() {
    const { ctx, app } = this
    const data = await app.mysql.select('hotRoom')
    const { authorization } = ctx.request.header

    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //获取新房详情数据。
  async Detail() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('hotRoom', { id })

    ctx.body = {
      code: 1,
      data
    }
  }


  //新房 户型筛选
  async screening() {
    const { app, ctx } = this
    const { type, orientation } = ctx.request.body
    //const data = await app.mysql.select("hotRoom", { where: { type: value } })
    const sql = {};
    type && (sql.type = type);
    orientation && (sql.orientation = orientation);
    console.log(sql);

    const results = await this.app.mysql.select('hotRoom', { // 搜索 post 表
      where: sql, // WHERE 条件
    });
    ctx.body = {
      code: 1,
      data: results,
    }
  }


  // 户型筛选  求租  求购
  async screening2() {
    const { app, ctx } = this
    const { type2, address } = ctx.request.body
    //const data = await app.mysql.select("hotRoom", { where: { type: value } })
    const sql = {};
    type2 && (sql.type2 = type2);
    address && (sql.address = address);
    console.log(sql);

    const results = await this.app.mysql.select('hotRoom', { // 搜索 post 表
      where: sql, // WHERE 条件
    });
    ctx.body = {
      code: 1,
      data: results,
    }
  }




  //消息列表   排序列表
  async newsList() {
    const { ctx, app } = this
    const data = await app.mysql.select('newsList')
    const { authorization } = ctx.request.header

    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }



  //排行排序搜索
  async rankingsearch() {
    const { app, ctx } = this
    const { value } = ctx.request.body
    const data = await app.mysql.query(`select * from newsList where name like "%${value}%"`)

    ctx.body = {
      code: 1,
      data
    }
  }


  //消息删除数据
  async newsDel() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('newsList', { id })

    ctx.body = {
      code: 1,
      data
    }
  }




  //渲染直播列表数据
  async liveList() {
    const { ctx, app } = this
    const data = await app.mysql.select('liveList')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }

  //渲染团购列表数据
  async tableData2() {
    const { ctx, app } = this
    const data = await app.mysql.select('tableData2')
    const { authorization } = ctx.request.header
    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }


  //获取团购详情数据。
  async Detail2() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('tableData2', { id })

    ctx.body = {
      code: 1,
      data
    }
  }



  //团购管理删除数据
  async tableDataDel2() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('tableData2', { id })

    ctx.body = {
      code: 1,
      data
    }
  }







  //楼市圈
  async housingRing() {
    const { ctx, app } = this
    const data = await app.mysql.select('housingRing')
    const { authorization } = ctx.request.header

    try {
      let res = jwt.verify(authorization, '1901A')
      ctx.body = {
        code: 1,
        msg: "成功",
        res,
        data
      }
    } catch (error) {
      ctx.body = {
        code: 0,
        msg: '登录失效，请重新登录'
      }
    }
  }



  //楼市圈删除数据
  async housingRingDel() {
    const { app, ctx } = this
    const { id } = ctx.request.body
    const data = await app.mysql.delete('housingring', { id })

    ctx.body = {
      code: 1,
      data
    }
  }


  //团购 开团
  async insert() {
    const { ctx, app } = this;
    const { userObj } = ctx.request.body;
    // console.log(userObj)
    const data = await app.mysql.insert(`tableData2`, { name: userObj.name, area: userObj.area, img: userObj.img, newtime: userObj.newtime, id: userObj.id, });
    ctx.body = {
      data
    }
  }

}

module.exports = HomeController;
